package cn.txm.quartz.service.impl;

import cn.txm.quartz.domain.QuartzJobInfo;
import cn.txm.quartz.service.IQuartzService;
import cn.txm.quartz.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;


    /**
     * 添加定时任务
     * @param quartzJobInfo
     */
    @Override
    public void add(QuartzJobInfo quartzJobInfo) {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        //调用工具类添加定时任务
        QuartzUtils.addJob(scheduler,quartzJobInfo.getJobName(),
                quartzJobInfo.getClz(),quartzJobInfo.getParams(),
                quartzJobInfo.getCronExpression());
    }

    /**
     * 删除定时任务
     * @param jobName
     */
    @Override
    public void remove(String jobName) {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        QuartzUtils.removeJob(scheduler,jobName);
    }
}
